package com.googlecode.spruce.plugins.core.network;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

import com.googlecode.spruce.core.DefaultPluginResult;
import com.googlecode.spruce.core.ServiceCheckPlugin;
import com.googlecode.spruce.core.ServiceCheckPluginResult;

public class DnsServiceCheck implements ServiceCheckPlugin
{
    private String host;
    private List<String> ipAddresses;

    @Override
    public ServiceCheckPluginResult execute()
    {
        try
        {
            final InetAddress address = InetAddress.getByName(this.host);
            final String hostAddress = address.getHostAddress();

            if (!ipAddresses.contains(hostAddress))
            {
                return new DefaultPluginResult(0, "Host name does not have expected IP address.  Expected: "
                        + this.ipAddresses.toString() + " Found: " + hostAddress);
            }
        }
        catch (final UnknownHostException uhe)
        {
            return new DefaultPluginResult("Could not determine IP address of: " + this.host);
        }

        return new DefaultPluginResult(100);
    }

    public String getHost()
    {
        return host;
    }

    public void setHost(final String host)
    {
        this.host = host;
    }

    public List<String> getIpAddresses()
    {
        return ipAddresses;
    }

    public void setIpAddresses(final List<String> ipAddresses)
    {
        this.ipAddresses = ipAddresses;
    }

}
